
package com.rx.mgt.controller;  

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.rx.mgt.domain.UserGroupDepartMent;
import com.rx.mgt.domain.UserInfoExt;
import com.rx.mgt.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rx.mgt.domain.AdminUser;
import com.rx.mgt.domain.UserQuotaGroup;
import com.rx.mgt.po.UsersBo;
import com.rx.mgt.util.IpUtil;

/** 
 * ClassName:UserQuotaGroupController 
 * Function :
 * Reason   : 
 * Date     :Jan 31, 2017 9:35:41 PM 
 * @author   luolei 
 * @since    JDK 1.6
 */
@Controller
public class UserQuotaGroupController {
    @Autowired
    UserQuotaGroupService userQuotaGroupService;
    @Autowired
    DomainService domainService;
    @Autowired
    UserInfoExtService userInfoExtService;
    @Autowired
    SysLogService sysLogService;

    @Autowired
    UserGroupDepartMentService userGroupDepartMentService;


    @RequestMapping("/users-group-list.htmlx")
    @ResponseBody
    public List<Map<String,Object>> getUsersGroupList(HttpServletRequest request,UsersBo usersBo){
        List<Map<String,Object>> rsList= new ArrayList<Map<String, Object>>();
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        if("2".equals(user.getType())){
            usersBo.setDomain(user.getDomain());
        }
        List<UserQuotaGroup> list = userQuotaGroupService.getAllQuotaGroup(usersBo);
        for(UserQuotaGroup userQuotaGroup:list){
            Map rs = new HashMap();
            rs.put("id",userQuotaGroup.getId());
            rs.put("pId",0);
            rs.put("name",userQuotaGroup.getName());
            rs.put("open",true);
            rsList.add(rs);
            List<Map<String,Object>> list1 = userQuotaGroupService.getGroupByQuotaGroupName(userQuotaGroup.getName());
            if(!CollectionUtils.isEmpty(list1)){
                for(Map<String,Object> m:list1){
                    m.put("pId",userQuotaGroup.getId());
                    m.put("parentName",userQuotaGroup.getName());
                    m.put("id",m.get("did"));
                    m.put("name",m.get("departmentName"));
                    m.remove("did");
                    m.remove("departmentName");
                    rsList.add(m);
                }

            }

        }

        return rsList;
    }

    /**
     * 添加或修改稿企业名称
     */
    @RequestMapping(value = "/addOrUpdateGroupName",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addOrUpdateGroupName(@RequestParam("id")String id,@RequestParam("pId")String pId,@RequestParam("name")String name){
        Map<String,Object> rs = new HashMap<String, Object>();
        int  i = userQuotaGroupService.addOrUpdateGroupName(id,pId,name);
        if(i>0){
            rs.put("msg","success");
        }else{
            rs.put("msg","error");
        }
        return rs;
    }
    /**
     * 添加企业
     */
    @RequestMapping("/addGroup.htmlx")
    @ResponseBody
    public Map<String,Object> addGroup(){
        Map<String,Object> rs = new HashMap<String, Object>();
        try {
            userQuotaGroupService.addGroup(rs);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  rs;

    }

    /**
     * 获取所有部门
     */

    @RequestMapping("/queryDepartMent.htmlx")
    @ResponseBody
    public List<UserGroupDepartMent> queryAllUserGroupDepartMents(HttpServletRequest request, UsersBo usersBo){
        String pId = request.getParameter("pId");
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        if("2".equals(user.getType())){
            usersBo.setDomain(user.getDomain());
        }
        return  userGroupDepartMentService.queryAllDepartmentsByGroupId(pId);

    }

    /**
     * 获取企业成员
     * @param model
     * @param request
     * @param usersBo
     * @return
     */
    @RequestMapping("/getGroupMembers.htmlx")
    @ResponseBody
    public Map<String,Object> getGroupMembers(@RequestParam("companyName")String companyName,@RequestParam(value = "departmentName",required = false)String departmentName){
        Map<String,Object> ps = new HashMap<String, Object>();
        try {
            List<Map<String,Object>> list = userQuotaGroupService.getGroupMembers(companyName,departmentName);
            ps.put("tableData",list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ps;

    }



    @RequestMapping(value="/users-quota-group-list.htmlx")
    public String  getAllGroup(ModelMap model,HttpServletRequest request ,UsersBo usersBo){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        if("2".equals(user.getType())){
            usersBo.setDomain(user.getDomain());
        }
        List list = userQuotaGroupService.getAllQuotaGroup(usersBo);
        model.addAttribute("list",list);
        model.addAttribute("usersBo",usersBo);
        System.out.println("111111111111111");
        return "user/users-quota-group-list";
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/users-quota-group-add.htmlx")
    public String  add(ModelMap model,HttpServletRequest request,UserQuotaGroup userQuotaGroup,UsersBo usersBo){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        if("2".equals(user.getType())){
            usersBo.setDomain(user.getDomain());
        }
        List list = domainService.getDomainInfoExt(usersBo);
        model.addAttribute("domainList",list);
        Map map =new HashMap();
        map.put("result", "success");
        if(userQuotaGroup.getId()==null||userQuotaGroup.getId()<1){
            model.addAttribute("userQuotaGroup",userQuotaGroup);
            model.addAttribute("method","add");
            return "user/users-quota-group-add";
        }
        UserQuotaGroup info= userQuotaGroupService.get(userQuotaGroup.getId());
        if(info!=null){
            model.addAttribute("group",info);
        }
        return "user/users-quota-group-add";
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/quotaGroupSave.htmlx")
    @ResponseBody
    public Map  quotaGroupSave(HttpServletRequest request ,ModelMap model,UserQuotaGroup userQuotaGroup){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("更新组", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        Map map =new HashMap();
        map.put("result", "success");
        userQuotaGroup.setStatus("1");
        userQuotaGroupService.saveOrUpdate(userQuotaGroup);
        map.put("id",userQuotaGroup.getId());
        return map;
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/deleteQuotaGroup.htmlx")
    @ResponseBody
    public Map  deleteQuotaGroup(HttpServletRequest request ,String ids){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("删除组", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        Map map =new HashMap();
        if(ids!=null){
            String[] id=ids.split(",");
            for(String s:id){
                userQuotaGroupService.delete(Long.valueOf(s));
            }
        }
        map.put("result", "success");
        System.out.println("111111111111111");
        return map;
    }
}
  